package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrdersSerice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

@ResponseBody
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrdersSerice ordersSerice;

    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders, HttpSession session) {
        Long userId = (Long) session.getAttribute("user");
        orders.setUserId(userId);
        ordersSerice.submit(orders);
        return R.success("下单成功");
    }

    @GetMapping("/userPage")
    public R<Page<Orders>> getUserOrder(HttpSession session, int page, int pageSize) {
        Long userId = (Long) session.getAttribute("user");
        return R.success(ordersSerice.pageOrder(userId,page,pageSize));
    }
    @GetMapping("/page")
    public R<Page<Orders>> getAllOrder(int page, int pageSize, Integer number, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
                                       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){

        return R.success(ordersSerice.pageAllOrder(page,pageSize,number,beginTime,endTime));
    }
    @PutMapping
    public R updateOrder(@RequestBody Map<String,Long> map){
        ordersSerice.updateStatusById(map.get("id"),map.get("status"));
        return R.success("更新订单状态成功");
    }
}
